/**
 * 
 */
package net.mikoo.seals.manage.web.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.model.Company;
import net.mikoo.seals.model.Party;
import net.mikoo.seals.model.UserLogin;

/**
 * @author huangxm
 *
 */
public class BaseController {
	
	protected static final String RESULT_CODE = "code";

	protected static final String RESULT_MESSAGE = "msg";

	protected static final String ERROR = "0";

	protected static final String SUCCESS = "1";
	
	protected static final String NOT_LOGIN = "-1";
	

	protected static final String MSG_SUCCESS = "操作成功";

	protected static final String MSG_SYSTEM_ERROR = "系统出错！请联系管理员";
	
	/**
	 * 跳转
	 * @param path
	 * @return
	 */
	protected String redirect(String path) {
		
		return "redirect:" + path;
	}
	
	
	protected String forward(String path) {
		
		return "forward:" + path;
	}
	
	
	/**
	 * 获取用户登录信息
	 * @param request
	 * @return
	 */
	protected UserLogin getLoginUser(HttpServletRequest request,String sessionKey) {
		
		Party party = getParty(request,sessionKey);
		if(party != null){
			return party.getUserLogin();
		}
		return null;
	}
	
	/**
	 * 获取当前登录的当事人信息
	 * @param request
	 * @return
	 */
	protected Party getParty(HttpServletRequest request,String sessionKey) {
		
		HttpSession session = request.getSession();
		Object obj = session.getAttribute(sessionKey);
		if (obj != null) {
			Party party= (Party) obj;
			return party;
		}
		return null;
	}
	
	
	protected Company getCompany(HttpServletRequest request) {
		
		HttpSession session = request.getSession();
		Object obj = session.getAttribute(WebContext.ADMIN_COMPANY_KEY);
		if (obj != null) {
			Company company= (Company) obj;
			return company;
		}
		return null;
	}
	
	/**
	 * 获取当前当事人编号
	 * @param request
	 * @return
	 */
	protected Long getCurrentPartyId(HttpServletRequest request,String sessionKey) {
		UserLogin userLogin = getLoginUser(request,sessionKey);
		if(userLogin ==null){
			return null;
		}
		return userLogin.getPartyId();
	}
	
	protected String getCurrentUserLoginId(HttpServletRequest request,String sessionKey) {
		UserLogin userLogin = getLoginUser(request,sessionKey);
		return userLogin.getUserLoginId();
	}
	
	
	protected Long getCompanyId(HttpServletRequest request,String sessionKey) {
		Company company = getCompany(request);
		if(company ==null){
			return null;
		}
		return company.getCompanyId();
	}

	
	/**
	 * 保存session
	 * @param userLogin
	 * @param request
	 */
	protected void retainSession(Party party, HttpServletRequest request,String sessiond) {
		
		if (party != null) {
			HttpSession session = request.getSession();
			session.setAttribute(sessiond,party);
		}
	}
	
	/**
	 * 删除session
	 * @param userLogin
	 * @param request
	 */
	protected void removeSession(HttpServletRequest request,String sessiond) {
		HttpSession session = request.getSession();
		Object object = session.getAttribute(sessiond);
		if (object != null) {
			session.removeAttribute(sessiond);
		}
	}
	
	
	protected Map<String, Object> result(String code,String msg) {
		
		Map<String, Object> webResult = new HashMap<>();
		webResult.put(RESULT_CODE, code);
		webResult.put(RESULT_MESSAGE, msg);
		return webResult;
	}
	
	protected Map<String, Object> result(String code, String msg, Object data) {

		Map<String, Object> webResult = new HashMap<>();
		webResult.put(RESULT_CODE, code);
		webResult.put(RESULT_MESSAGE, msg);
		webResult.put("data", data);
		return webResult;
	}
	
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
	    /**
	      * 自动转换日期类型的字段格式
	      */
	    binder.registerCustomEditor(Date.class, new SpecialDateEditor());
	}
	
	
}
